<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/18
 * Time: 13:44
 */
namespace work{
    use app\ctr;
    use C\mod\order;
    use C\lib\Page;
    use C\lib\Db;
    use C\lib\verification;
    use C\lib\Uploads;
    use C\lib\Oss;
    use C\mod\worker;
    use C\mod\workerd;
    use C\mod\user;
    use C\mod\wprice;
    use C\mod\proute;
    use C\lib\Wxapi;

    class orders extends ctr{
        private function orderList($state){

            if(($post=$this->getAesPost()) && ($worker=$this->getLogWorker())) {

                if (isset($post['lastId']) && trim($post['lastId']) != '') {
                    $lastId = trim($post['lastId'] + 0);
                } else {
                    $lastId = 0;
                }
                if($post['lastId'] == 0) {
                    $lastId = ['id','>',$lastId];
                } else {
                    $lastId = ['id','<',$lastId];
                }
                $data= [];
                //$list = order::getList(['worker_id' => 20,['state','in',$type]]);
                $list = order::getList(['worker_id'=>$worker->id,['state','in',$state],$lastId],[0,10],['id','DESC']);
                foreach ($list as $value) {
                    $userT=user::get(['id'=>$value->user_id]);
                    $path=$userT->path;
                    $workerd=workerd::get(['worker_id'=>$worker->id]);
                    if($workerd->type==0){
                        $data[] = [
                            'id' => $value->id,
                            'address_sender' => $value->address_recipient,
                            'phone_sender' => $value->phone_recipient,
                            'name_sender' => $value->name_recipient,
                            'states' => $value->state,
                            'head_oss' => $path,
                            'remark' =>$value->remark,
                            'identity' => $worker->state,
                            'ex_order' => $value->ex_order,
                            'lat' => $value->lat,
                            'lng' => $value->lng,
                            'time_add' => $value->time_add
                        ];
                    }else{
                        $data[] = [
                            'id' => $value->id,
                            'address_sender' => $value->address_sender,
                            'phone_sender' => $value->phone_sender,
                            'name_sender' => $value->name_sender,
                            'states' => $value->state,
                            'head_oss' => $path,
                            'remark' =>$value->remark,
                            'identity' => $worker->state,
                            'ex_order' => $value->ex_order,
                            'lat' => $value->lat,
                            'lng' => $value->lng,
                            'time_add' => $value->time_add
                        ];
                    }
                }
                $this->writeAesJson(1,['data'=>$data],1);
            }
        }
        /*
         * 未处理订单列表
         */
        public function untreated(){
            $state = [1,2];


            $this->orderList($state);
        }
        /*
         * 已处理订单列表
         */
        public function processed(){
            $state=[5];
            $this->orderList($state);
        }
        /*
         * 测试订单处理
         */
        public function h(){
            Db::connect();
            $new_array= [];
            $proute=new proute();
            $wprice=wprice::get(['worker_id'=>15]);
            //var_dump($wprice->price);exit;
            $post['data']=[
                '0'=>[
                    'id'=>'1',
                    'price'=>'5',
                    'ex_order'=>'c',
                    'weight'=>6,
                    'path_oss'=>'51166000'
                ],
                '1'=>[
                    'id'=>'2',
                    'price'=>'88',
                    'ex_order'=>'c',
                    'weight'=>10,
                    'path_oss'=>'330011'
                ],
            ];
            //echo json_encode($post);exit;
            $b=[
                'state'=>1,
                'id'=>15
            ];
            $worker=json_encode($b);
            $pay_type=3;
            $pay_state=1;
            $time=$_SERVER['REQUEST_TIME'];
            if($post['data']){
                foreach ($post['data'] as $v){

                    if($this->checkArray($v,$worker)){

                        $order=order::get(['id'=>$v['id']]);
                        if(1==1){
                            $data=[
                                'ex_order'=>$v['ex_order'],
                                'weight'=>$v['weight'],
                                'price'=>$v['price'],
                                'path_oss'=>$v['path_oss'],
                                'time_update'=>$_SERVER['REQUEST_TIME'],
                                'state'=>2
                            ];
                        }else{
                            $data=[
                                'ex_order'=>$v['ex_order'],
                                'standard'=>$v['standard'],
                                'price'=>$v['price'],
                                'path_oss'=>$v['path_oss'],
                                'time_update'=>$_SERVER['REQUEST_TIME'],
                                'state'=>2
                            ];
                        }
                        //print_r($data);//exit;
                        $order->set($data);
                        //数据
                        $m=[
                            'worker_id'=>15,
                            'price'=>$v['price'],
                            'pay_type'=>$pay_type,
                            'pay_state'=>$pay_state,
                            'time_add'=>$time
                        ];
                        print_r($m);//exit;
                        $proute->set($m);

                        $wprice->set([
                            'price'=>7+$v['price']
                        ]);
                        try {
                            Db::beginTransaction();
                            $orderup=$order->update();
                            $prouteadd=$proute->create();
                            $wpriceup=$wprice->update();

                            if ($orderup && $prouteadd && $wpriceup) {
                                Db::commit();
                                //$res=true;
                                //$this->writeAesJson(1,'提交成功!',1);
                            } else {
                                Db::rollBack();
                                // $res=true;
                                //$this->writeAesJson(0,'提交失败!',1);
                            }
                        } catch (\Exception $e) {
                            Db::rollBack(); // 执行失败，事务回滚
                            //$res=true;
                            // $this->writeAesJson(0,'提交失败!',1);
                        }
                    }else{
                        array_push($new_array,$value);
                    }
                    if(empty($new_array)){
                        echo "成功";
                        //$this->writeAesJson(1,'提交成功',1);
                    }else{
                        echo 22;
                       //print_r($new_array);
                        //$this->writeAesJson(0,['new_array'=>$new_array],1);
                    }
                }
            }else{
                echo '空';
            }


        }
        /************/

        public function handle(){
            //$new_array = [];
            Db::connect();
            //$order=New \C\mod\order();
            if($list=$this->checkArray1($_POST,14)){
                $proute=new \C\mod\proute();
                $wprice=wprice::get(['worker_id'=>14]);
                $order=order::get(['id'=>$list['id']]);
                $order->set($list);
                //数据
                $proute->set([
                    'worker_id'=>14,
                    'price'=>$list['price'],
                    'pay_type'=>3,
                    'pay_state'=>1,
                    'time_add'=>$_SERVER['REQUEST_TIME']

                ]);
                $wprice->set([
                    'price'=>$wprice->price+$list['price']
                ]);
                try {
                    Db::beginTransaction();
                    $orderup=$order->update();
                    $prouteadd=$proute->create();
                    $wpriceup=$wprice->update();

                    if ($orderup && $prouteadd && $wpriceup) {
                        Db::commit();
                        echo "成功";
                    } else {
                        Db::rollBack();
                       echo "回滚";
                    }
                } catch (\Exception $e) {
                    Db::rollBack(); // 执行失败，事务回滚
                    echo "回滚";
                }
            }else{
                echo 44;
            }
        }
        /*
         * 验证订单是否满足条件
         */
        public function checkArray($data,$workerd){
           // print_r($data);

           // $this->writeAesJson(0,$data['weight'],1);exit;

            if(empty($data['id'])){
                $this->writeAesJson(0,'请选择要处理的订单!',1);exit;
            }
            if($workerd->type==1){
                if(!empty($data['weight']) && verification::isNum($data['weight'])){
                }else{
                    $this->writeAesJson(0,'请输入正确格式的重量!',1);exit;
                }
            }else{
                if(!empty($data['standard']) && verification::isOne($data['standard'])){
                }else{
                    $this->writeAesJson(0,'请选择标准!',1);exit;
                }
            }

            if(!empty($data['ex_order'])){
            }else{
                $this->writeAesJson(0,'请输入快递单号!',1);exit;
            }

            if(!empty($data['price'])){
                if(verification::isNum($data['price'])){
                }else{
                    $this->writeAesJson(0,'请填写正确格式的价格!',1);exit;
                }
            }else{
                $this->writeAesJson(0,'请填写价格!',1);exit;
            }
            if(!empty($data['path_oss'])){
                if (strlen($data['path_oss']) <= 2097152) {

                    Oss::connect();
                    do {
                        $oss_hand = 'upload/img/' . date('Y') . '/' . date('m') . '/' . md5($workerd->worker_id . uniqid() . rand(1000, 9999)) . '.jpg';
                    } while (Oss::isExist($oss_hand));
                    if(Oss::putFile($oss_hand, base64_decode($data['path_oss']))){

                    }else{
                        $this->writeAesJson(0,'图片上传失败!',1);exit;
                    }
                }else{
                    //$this->writeAesJson(0,'上传图片失败',1);exit;
                    $this->writeAesJson(0,'图片大于规定大小!',1);exit;
                }
            }else{
                //$this->writeAesJson(0,'上传图片失败',1);exit;
                $this->writeAesJson(0,'请上传图片!',1);exit;
            }

            return $oss_hand;
        }
        /*
         * 处理订单
         */
        public function handleOrder(){
            if(($post=$this->getAesPost()) && ($worker=$this->getLogWorker())){
//                $this->writeAesJson(1,['post'=>$post],1);
                $new_array= [];

//                $wprice=wprice::get(['worker_id'=>$worker->id]);


                if($post['data']){
                    foreach ($post['data'] as $v){
                        $workerd=workerd::get(['worker_id'=>$worker->id]);

                        if($oss_hand=$this->checkArray($v,$workerd)){

                            $order=order::get(['id'=>$v['id']]);
                            if($order->state!=1){
                                $this->writeAesJson(0,'此单已经无效！',1);exit;
                            }else{
                                //$this->writeAesJson(0,$v['weight'].'kk',1);exit;
                                if($workerd->type==1){//邮寄
                                    $data=[
                                        'ex_order'=>$v['ex_order'],
                                        'weight'=>$v['weight'],
                                        'price'=>$v['price'],
                                        'path_oss'=>$oss_hand,
                                        'state'=>2,
                                        'time_update'=>$_SERVER['REQUEST_TIME']
                                    ];
                                }else{//代取
                                    $data=[
                                        'ex_order'=>$v['ex_order'],
                                        'standard'=>$v['standard'],
                                        'price'=>$v['price'],
                                        'path_oss'=>$oss_hand,
                                        'state'=>2,
                                        'time_update'=>$_SERVER['REQUEST_TIME'],
                                    ];
                                }
                                $order->set($data);
//                                $proute=new proute();
//                                //数据
//                                $proute->set([
//                                    'worker_id'=>$worker->id,
//                                    'price'=>$v['price'],
//                                    'pay_type'=>3,
//                                    'pay_state'=>1,
//                                    'time_add'=>$_SERVER['REQUEST_TIME']
//
//                                ]);
//                                $wprice->set([
//                                    'price'=>$wprice->price+$v['price']
//                                ]);

                                try {
                                    Db::beginTransaction();
                                    $orderup=$order->update();
//                                    $prouteadd=$proute->create();
//                                    $wpriceup=$wprice->update();
                                    //给用户推送消息
                                    $user=user::get(['id'=>$order->user_id]);
                                    //获取快递员名字
                                    //$worderd=workerd::get(['worker_id'=>$worker->id]);
                                    $sendInfo=Wxapi::sendInfo($user->wx_openid,$order->type,$order->order_id,2,$workerd->realname,'',$v['id'],$v['price'],$_SERVER['REQUEST_TIME']);

                                    if ($orderup && $sendInfo=="ok") {

                                        Db::commit();
                                        $this->writeAesJson(1,1);exit;
                                    } else {
                                        Db::rollBack();
                                        $this->writeAesJson(0,'处理失败!',1);exit;
                                    }
                                } catch (\Exception $e) {
                                    Db::rollBack(); // 执行失败，事务回滚
                                    $this->writeAesJson(0,'处理失败!',1);exit;
                                }
                            }
                        }else{
                            $this->writeAesJson(0,'处理失败!',1);exit;
                        }
                    }
                }else{
                    $this->writeAesJson(0,'订单不能为空',1);exit;
                }
            }
        }

        /*
         * 快递员(取递员接单)
         */
        public function getOrder(){
            if(($post=$this->getAesPost()) && ($worker=$this->getLogWorker())){

                if(!empty($post['id'])){
                }else{
                    $this->writeAesJson(0,'缺少id参数!',1); exit;
                }
                $order=order::get(['id'=>$post['id']]);
                if($order->state!=0){
                    $this->writeAesJson(0,'此单已经无效！',1);exit;
                }
                //获取快递员名字
                $worderd=workerd::get(['worker_id'=>$worker->id]);

                $express_name=workerd::get(['worker_id'=>$worker->id]);
                $order->set([
                    'state'=>1,
                    'worker_id'=>$worker->id,
                    'express_name'=>$express_name->subsidiary,
                    'time_update'=>$_SERVER['REQUEST_TIME'],

                ]);
                if($order->update()){
                    //给用户推送消息
                    $user=user::get(['id'=>$order->user_id]);
                    //获取快递员名字
                    $worderd=workerd::get(['worker_id'=>$worker->id]);
                    //$res=Wxapi::sendInfo($user->wx_openid,'',$order->order_id,$worderd->realname,'','',$_SERVER['REQUEST_TIME']);
                    $res=Wxapi::sendInfo($user->wx_openid,'',$order->order_id,1,$worderd->realname,$worderd->phone,'','','');
                    //var_dump($a);exit;
                    if($res=="ok"){
                        $this->writeAesJson(1,1);exit;
                    }else{
                        $order->set([
                            'state'=>0,
                            'worker_id'=>0,
                            'express_name'=>'',
                            'time_update'=>$_SERVER['REQUEST_TIME'],

                        ]);
                        $order->update();
                        $this->writeAesJson(0,'接单失败!',1);
                    }
                }else{
                    $this->writeAesJson(0,'接单失败!',1);
                }
            }
        }
        /*
         *快递订单详情
         */
        public function details(){

            if(($post=$this->getAesPost()) && ($worker=$this->getLogWorker())){
                if(!empty($post['id'])){
                }else{
                    $this->writeAesJson(0,'缺少id参数!',1); exit;
                }
                $data=[];
                $res=order::get(['id'=>$post['id']]);
                $data[]=[
                    'ex_order'=>$res->order_id,
                    'address_sender'=>$res->address_sender,
                    'phone_sender'=>$res->phone_sender,
                    'name_sender'=>$res->name_sender,
                    'address_recipient'=>$res->address_recipient,
                    'phone_recipient'=>$res->phone_recipient,
                    'name_recipient'=>$res->name_recipient,
                    'goods_type'=>$res->goods_type,
                    'weight'=>$res->weight,
                    'price'=>$res->price,
                    'path_oss'=>STATIC_PATH.$res->path_oss
                ];
                $this->writeAesJson(1,[
                    'data'=>$data
                ],1);
            }
        }
        /*
         * 取快递订单详情
         */
        public function detail(){

            if(($post=$this->getAesPost()) && ($worker=$this->getLogWorker())){
                if(!empty($post['id'])){
                }else{
                    $this->writeAesJson(0,'缺少id参数!',1); exit;
                }
                $data=[];
                $res=order::get(['id'=>$post['id']]);
                $data[]=[
                    'ex_order'=>$res->order_id,
                    'address_sender'=>$res->address_sender,
                    'phone_sender'=>$res->phone_sender,
                    'name_sender'=>$res->name_sender,
                    'address_recipient'=>$res->address_recipient,
                    'phone_recipient'=>$res->phone_recipient,
                    'name_recipient'=>$res->name_recipient,
                    'standard'=>$res->standard,
                    'price'=>$res->price,
                    'path_oss'=>STATIC_PATH.$res->path_oss
                ];
                $this->writeAesJson(1,[
                    'data'=>$data
                ],1);
            }
        }
    }
}